﻿using Inovout.YunYao.Models;
using NHibernate;
using NHibernate.Criterion;
using System.Collections;
using System.Collections.Generic;

namespace Inovout.YunYao.Repositories
{
    public class MonitoringNodeRepository : MonitoringElementRepository<MonitoringNode>
    {
        public IList<IDictionary> GetSolutionStatistic()
        {
            using (ISession session = OpenSession())
            {
                return session.CreateCriteria<MonitoringNode>()
                    .CreateCriteria("Solution", "solution")
                    .SetProjection(Projections.ProjectionList()
                        .Add(Projections.GroupProperty("Solution.Id"), "SolutionId")
                        .Add(Projections.GroupProperty("solution.Name"), "SolutionName")
                        .Add(Projections.Count("Solution.Id"), "NodeCount")
                    )
                    .AddOrder(Order.Desc("NodeCount"))
                    .SetResultTransformer(CriteriaSpecification.AliasToEntityMap)
                    .List<IDictionary>();
            }
        }
        public IList<IDictionary> GetLocationStatistic()
        {
            using (ISession session = OpenSession())
            {
                return session.CreateCriteria<MonitoringNode>()
                    .CreateCriteria("Location", "location")
                    .SetProjection(Projections.ProjectionList()
                        .Add(Projections.GroupProperty("location.Id"), "LocationId")
                        .Add(Projections.GroupProperty("location.Name"), "LocationName")
                        .Add(Projections.Count("location.Id"), "NodeCount")
                    )
                    .AddOrder(Order.Desc("NodeCount"))
                    .SetResultTransformer(CriteriaSpecification.AliasToEntityMap)
                    .List<IDictionary>();
            }
        }
    }
}
